package com.chmingx.crud.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chmingx.crud.bean.User;
import com.chmingx.crud.mapper.UserMapper;
import com.chmingx.crud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/user")
    public String getAllUser() {
        List<User> userList = userService.list();
        // 使用fastjson将列表转为json
        return JSON.toJSONString(userList);
    }

    @GetMapping("/userpage")
    public String getUserByPage(@RequestParam("currentPage") int currentPage,
                                @RequestParam("pageSize") int pageSize) {
        // 构造分页参数
        Page<User> page = new Page<>(currentPage, pageSize);
        // 调用page进行分页
        Page<User> userPage = userService.page(page, null);
        System.out.println("当前页数: " + userPage.getCurrent());
        System.out.println("总页数: " + userPage.getPages());
        System.out.println("总记录数: " + userPage.getTotal());
        return JSON.toJSONString(userPage.getRecords()); // 获取当前页记录
    }
}
